# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from openerp import models, fields, api

class product_product(models.Model):

    _inherit = 'product.product'

    level = fields.Char(string=u'产品质量级别', translate=True)

    @api.one
    @api.returns('product.product')
    def search_or_create_level(self,level=''):
        domain = [('product_tmpl_id', '=', self.product_tmpl_id.id),('level','=',level)]
        for attribute_value_id in self.attribute_value_ids.ids:
            domain.append(('attribute_value_ids', '=', attribute_value_id))
        product = self.search(domain)
        if product:
            return product
        new_product = self.create({
            'product_tmpl_id': self.product_tmpl_id.id,
            'attribute_value_ids': [(6, 0, self.attribute_value_ids.ids)]})
        new_product.level = level
        new_product.uos_id=self.uos_id
        print(self.product_tmpl_id)
        print(new_product.product_tmpl_id)
        print('create new product level ',new_product,level)
        return new_product
